/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jxtafileshare;

import java.io.*;
import net.jxta.document.AdvertisementFactory;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;
/**
 *
 * @author Danny
 */
public class FileFinder {
    private File[] files = null;

    public boolean findFiles() {
        boolean found = false;
        String fileStr = "";
        File file = new File("C:\\Documents and Settings\\cs47402\\Desktop\\stuff");
        files = file.listFiles();
        if(files != null) {
            found = true;
        }
        return found;
    }

    public File[] getFiles() {
        if(files != null) {
            return files;
        } else {
            return null;
        }
    }

    public static PipeAdvertisement createFileAdvertisement(File file, PeerGroupID id) {
        String delim = "\\.";
        String fName = file.getName();
        String[] parts = fName.split(delim);
        PipeAdvertisement adv = (PipeAdvertisement)
                AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());
        adv.setPipeID(IDCreator.createNewPipeID(id));
        adv.setType(PipeService.UnicastType);
        adv.setName(parts[0]);
        adv.setDescription(parts[1]);
        return adv;
    }

    public static byte[] getBytes(File file) throws IOException {
        InputStream is = new FileInputStream(file);

        long length = file.length();

        if(length > Integer.MAX_VALUE) {
            System.out.println("File too large");
        }

        byte[] bytes = new byte[(int)length];

        int offset = 0;
        int numRead = 0;

        while (offset < bytes.length
            && (numRead = is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }

        if(offset < bytes.length) {
            throw new IOException("Could not read in" +
                    "entire file");
        }

        is.close();
        return bytes;
    }
}
